using System;
using System.Timers;
using CalcSharp.Core;

namespace CalcSharp
{
    class TimerUI
    {
        private TimerUI() { }
        public static void TimerSet(string[] param)
        {
            int tnum = (int)MiscFuncts.parsestr(param[1]);
            double ival = MiscFuncts.parsestr(param[2]);
            Program.loaded.TMan.SetTimerIntervall(tnum, ival);
            Program.loaded.TMan.SetTimerCode(tnum, param[3]);
        }

        public static void TimerStrart(string[] param)
        {
            if (param.Length > 5) throw new CSException("Too many parameters");
            if (param.Length < 6)
            {
                for (int i = 1; i < param.Length; i++)
                {
                    Program.loaded.TMan.StartTimer((int)MiscFuncts.parsestr(param[i]));
                }
            }
        }

        public static void TimerStop(string[] param)
        {
            if (param.Length > 5) throw new CSException("Too many parameters");
            if (param.Length < 6)
            {
                for (int i = 1; i < param.Length; i++)
                {
                    Program.loaded.TMan.StopTimer((int)MiscFuncts.parsestr(param[i]));
                }
            }
        }

        public static void ClearTimer(string[] param)
        {
            if (param.Length > 5) throw new CSException("Too many parameters");
            if (param.Length < 6)
            {
                for (int i = 1; i < param.Length; i++)
                {
                    Program.loaded.TMan.ClearTimerCode((int)MiscFuncts.parsestr(param[i]));
                }
            }
        }
    }

    class TimerMan
    {
        private Timer[] timers;
        private string[] timerfuncts;

        public TimerMan()
        {
            timers = new Timer[4];
            timerfuncts = new string[4];
            for (int i = 0; i < 4; i++)
            {
                timers[i] = new Timer();
                timers[i].Enabled = false;
                timers[i].Stop();
            }
            timers[0].Elapsed += new ElapsedEventHandler(T0Tick);
            timers[1].Elapsed += new ElapsedEventHandler(T1Tick);
            timers[2].Elapsed += new ElapsedEventHandler(T2Tick);
            timers[3].Elapsed += new ElapsedEventHandler(T3Tick);
        }

        private void T0Tick(object sender, ElapsedEventArgs e)
        {
            string code;
            if (!string.IsNullOrEmpty(timerfuncts[0]))
            {
                code = Program.functionmem[timerfuncts[0]].code;
                Program.loaded.ExecCommands(code);
            }
        }

        private void T1Tick(object sender, ElapsedEventArgs e)
        {
            string code;
            if (!string.IsNullOrEmpty(timerfuncts[1]))
            {
                code = Program.functionmem[timerfuncts[1]].code;
                Program.loaded.ExecCommands(code);
            }
        }

        private void T2Tick(object sender, ElapsedEventArgs e)
        {
            string code;
            if (!string.IsNullOrEmpty(timerfuncts[2]))
            {
                code = Program.functionmem[timerfuncts[2]].code;
                Program.loaded.ExecCommands(code);
            }
        }

        private void T3Tick(object sender, EventArgs e)
        {
            string code;
            if (!string.IsNullOrEmpty(timerfuncts[3]))
            {
                code = Program.functionmem[timerfuncts[3]].code;
                Program.loaded.ExecCommands(code);
            }
        }

        public void SetTimerIntervall(int timernum, double intervall)
        {
            if (intervall < 0.5) throw new Exception("Timer intervall must be greater than 0.5");
            if (timernum > 4 || timernum < 1) throw new CSException("Timer number must be between 1 and 4");
            this.timers[timernum - 1].Interval = (int)Math.Round((intervall * 1000), 0);
        }

        public void StartTimer(int timernum)
        {
            if (timernum > 4 || timernum < 1) throw new CSException("Timer number must be between 1 and 4");
            this.timers[timernum - 1].Enabled = true;
            this.timers[timernum - 1].Start();
        }

        public void StopTimer(int timernum)
        {
            if (timernum > 4 || timernum < 1) throw new CSException("Timer number must be between 1 and 4");
            this.timers[timernum - 1].Enabled = false;
            this.timers[timernum - 1].Stop();
        }

        public void SetTimerCode(int timernum, string Functionname)
        {
            if (timernum > 4 || timernum < 1) throw new CSException("Timer number must be between 1 and 4");
            if (string.IsNullOrEmpty(Functionname)) throw new CSException("Timer function must have a name");
            if (!Program.functionmem.isset(Functionname)) throw new CSException("Invalid timer function");
            this.timerfuncts[timernum - 1] = Functionname.ToUpper();
        }

        public void ClearTimerCode(int timernum)
        {
            if (timernum > 4 || timernum < 1) throw new CSException("Timer number must be between 1 and 4");
            this.timers[timernum - 1].Stop();
            this.timers[timernum - 1].Enabled = false;
            this.timerfuncts[timernum - 1] = null;
        }
    }
}